import { IMiddleware, Middleware, NextFunction } from '@midwayjs/core'
import { Context } from '@midwayjs/koa'

@Middleware()
export class ResponseMiddleware implements IMiddleware<Context, NextFunction> {
  static getName() {
    return ResponseMiddleware.name
  }

  resolve() {
    return async (ctx: Context, next: NextFunction) => {
      const data = await next()

      return {
        status: 200,
        message: 'ok',
        data
      }
    }
  }

  match(ctx: Context) {
    const isApi = ctx.path.startsWith('/api')
    const isNotBinary = ['/export'].findIndex(v => ctx.path.includes(v)) === -1

    return isApi && isNotBinary
  }
}
